﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Brettle.Web.NeatUpload;
using QuickORM.BLL;
using QuickORM.Entity;

/// <summary>
///     MultiFileHelp 的摘要说明
/// </summary>
public class MultiFileHelp
{
    private int filescount;
    private List<T_SystemAccessorie> tempfiles = new List<T_SystemAccessorie>();

    public EnumMultiFile Upload(MultiFile multifile, Multi multi)
    {
        filescount = 0;
        tempfiles = new List<T_SystemAccessorie>();

        #region

        foreach (var item in multifile.Files)
        {
            // 上传文件。
            try
            {
                var FileName = item.FileName; //获取上传文件的文件名,包括后缀
                var ExtenName = Path.GetExtension(FileName); //获取扩展名
                var filePath = HttpContext.Current.Request.MapPath("~/UpLoadFiles/" + multi.PathFile + "");

                if (ExtenName != ".pdf")
                {
                    return EnumMultiFile.alert;
                }

                var fileName = FileName.Substring(0, FileName.IndexOf('.')) + "_" + DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName;
                var fileURL = Path.Combine(filePath, fileName);

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                item.SaveAs(fileURL);
                filescount++;
                var tempfile = new T_SystemAccessorie
                {
                    FileName = fileName,
                    FilePath = fileURL,
                    TableName = multi.TableName,
                    ColName = multi.ColName,
                    DataID = multi.NewID
                };
                tempfiles.Add(tempfile);
            }
            catch (Exception)
            {
                //   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('上传文件失败！');", true);
                return EnumMultiFile.Ex;
            }
        }
        Add(tempfiles);
        return EnumMultiFile.Ok;

        #endregion
    }

    public void Add(List<T_SystemAccessorie> ls)
    {
        foreach (var item in ls)
        {
            BLLFactory<T_SystemAccessorieManager>.Instance.Insert(item);
        }
    }
}


public enum EnumMultiFile
{
    alert = 0,
    Ok = 1,
    Ex = 2
}

public class Multi
{
    public string PathFile { get; set; }
    public int NewID { get; set; }
    public string ColName { get; set; }
    public string TableName { get; set; }
}